<template>
  <div>
    <el-dialog :title="maintitle" :visible.sync="dialogVisible" width="60%">
      <el-form labelWidth="100px">
        <el-form-item label="人员姓名" class="is-required">
          <searchperson @getpersonid="getpersonid" :itempersonid="statusData.hash.pid"></searchperson>
          <div class="el-form-item__error" v-if="!$v.statusData.hash.pid.required">
            请选择人员姓名
          </div>
        </el-form-item>
        <el-form-item label="专业类别" class="is-required">
          <el-input v-model="statusData.hash.majortype" clearable></el-input>
          <div class="el-form-item__error" v-if="!$v.statusData.hash.majortype.required">
            请输入专业类别
          </div>
        </el-form-item>
        <el-form-item label="证书名称" class="is-required">
          <el-input v-model="statusData.hash.cername" clearable></el-input>
          <div class="el-form-item__error" v-if="!$v.statusData.hash.cername.required">
            请输入证书名称
          </div>
        </el-form-item>
        <el-form-item label="证书编号" class="is-required">
          <el-input v-model="statusData.hash.cernum" clearable></el-input>
          <div class="el-form-item__error" v-if="!$v.statusData.hash.cernum.required">
            请输入证书编号
          </div>
        </el-form-item>
        <el-form-item label="资格时间" class="is-required">
          <el-date-picker
            v-model="statusData.hash.grantime"
            style="width:100%"
            type="date"
            placeholder="开始日期"
            valueFormat="yyyy-MM-dd"
            clearable
          ></el-date-picker>
          <el-date-picker
            v-model="statusData.hash.validtime"
            style="width:100%"
            type="date"
            placeholder="截止日期"
            valueFormat="yyyy-MM-dd"
            clearable
          >
          </el-date-picker>
          <div class="el-form-item__error" v-if="!$v.statusData.hash.grantime.required">
            请选择资格开始时间
          </div>
        </el-form-item>
        <el-form-item label="附件">
          <upload2 :filelimit="5" :filelink.sync="filelink" :ispic="false" :isedit="false"></upload2>
        </el-form-item>
        <el-form-item>
          <div>&nbsp;</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addstatus">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import searchperson from '~/components/common/searchperson.vue';
import upload2 from '~/components/common/upload2.vue';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    statusData: {
      hash: {
        pid: {
          required,
        },
        majortype: {
          required,
        },
        cername: {
          required,
        },
        grantime: {
          required,
        },
        cernum: {
          required,
        },
      },
    },
  },
  data() {
    return {
      dialogVisible: true,
      ts: this.newStore(),
      disable: false,
      maintitle: '添加资格',
      statusData: {
        cols: [],
        hash: {
          username: '',
          cername: '',
          cernum: '',
          grantime: '',
          validtime: '',
          majortype: '',
          pid: '',
          id: '',
        },
      },
      // entrttimearr: [],
      filelink: [],
      changeval: 0,
      statuslist: [],
      fielname: [],
      fileurl: [],
    };
  },
  props: {
    // itemdata: Array,
    itemvalname: Object,
    chousebtn: Number,
  },
  components: {
    upload2,
    searchperson,
  },
  created() {
    this.getstatusval();
    if (this.chousebtn == 2) {
      this.maintitle = '修改资格';
      this.disable = true;
      this.fileminlink = [];
      this.filelink = [];
      // console.log(this.itemvalname, "修改个人业绩信息");
      const req = {
        handle: '0E56125B90D2898ACAD2C3968249185CCD3A43E9777C4E90',
        filetype: 'qualify',
        mainid: this.itemvalname.hash.id,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.fileminlink = ts.d.data.operation1;

        for (let i = 0; i < this.fileminlink.length; i += 1) {
          const a = {};
          a.name = this.fileminlink[i].hash.filename;
          a.url = this.fileminlink[i].hash.fileurl;
          this.filelink.push(a);
        }
      });
    }
  },
  mounted() {
    this.getstatusval();
  },
  methods: {
    // 公共方法图片数组转路径数组
    posturl(arr) {
      const a = [];
      for (let i = 0; i < arr.length; i += 1) {
        a.push(arr[i].url);
      }
      return a;
    },
    // 公共方法图片数组转名称数组
    postname(arr) {
      const b = [];
      for (let i = 0; i < arr.length; i += 1) {
        b.push(arr[i].name);
      }
      return b;
    },
    // 渲染信息
    getstatusval() {
      // console.log(this.itemvalname, "222");
      if (this.itemvalname == '' || this.itemvalname == undefined) {

      } else if (this.chousebtn == 2) {
        this.statusData = {};
        // console.log(this.itemvalname, "1");
        // console.log(this.statusData, "2");
        this.statusData = this.itemvalname;
        // console.log(this.statusData, "3");
      }
    },
    // 获取姓名
    getpersonid(val) {
      this.changeval = 1;
      this.statusData.hash.pid = val.id;
      this.statusData.hash.username = val.value;
    },
    // 增加/编辑合同信息
    addstatus() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else if (this.chousebtn == 1) {
        this.$confirm('是否添加该职业资格信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const req = {
              handle:
                '1CB43CC1631B5D5B8DDD57687CDE4AC73F156CD5A96D26B975CF9C8A35D44EF2',
              username: this.statusData.hash.username,
              cername: this.statusData.hash.cername,
              cernum: this.statusData.hash.cernum,
              // grantime: this.entrttimearr[0],
              // validtime: this.entrttimearr[1],
              grantime: this.statusData.hash.grantime,
              validtime: this.statusData.hash.validtime,
              majortype: this.statusData.hash.majortype,
              pid: this.statusData.hash.pid,
              fielname: this.postname(this.filelink),
              fileurl: this.posturl(this.filelink),
            };
            this.spost(this.ts, '/mainservlet', req).then((ts) => {
              // console.log(req, "111");
              this.$message({
                type: 'success',
                message: '添加成功!',
              });
              this.$parent.getstatuslist();
              this.dialogVisible = false;
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消',
            });
          });
      } else if (this.chousebtn == 2) {
        this.$confirm('是否修改该职业资格信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const req = {
              handle:
                '1CB43CC1631B5D5B8DDD57687CDE4AC73F156CD5A96D26B90382D6715B544D49',
              username: this.statusData.hash.username,
              cername: this.statusData.hash.cername,
              cernum: this.statusData.hash.cernum,
              pid: this.statusData.hash.pid,
              id: this.statusData.hash.id,
              // grantime: this.entrttimearr[0],
              // validtime: this.entrttimearr[1],
              grantime: this.statusData.hash.grantime,
              validtime: this.statusData.hash.validtime,
              majortype: this.statusData.hash.majortype,
              pid: this.statusData.hash.pid,
              fielname: this.postname(this.filelink),
              fileurl: this.posturl(this.filelink),
            };
            this.spost(this.ts, '/mainservlet', req).then((ts) => {
              // console.log(ts.d);
              this.$message({
                type: 'success',
                message: '修改成功!',
              });
              this.dialogVisible = false;
              // this.$emit("update:addstatus", this.statusData);
              this.$parent.getstatuslist();
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消',
            });
          });
      }
    },
  },
};
</script>


<style scoped>
.el-form-item {
    width: 50%;
    margin-bottom: 22px;
    float: left;
}
</style>
